package com.ctshk.rpc.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.constant.RedisConstants;
import com.ctshk.common.dto.Result;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.system.dto.OperationLogDTO;
import com.ctshk.rpc.system.entity.OperationLog;
import com.ctshk.rpc.system.mapper.OperationLogMapper;
import com.ctshk.rpc.system.req.OperationLogReq;
import com.ctshk.rpc.system.service.IOperationLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.ArrayList;
import java.util.List;


/**
 * @description 操作日志服务实现
 * @author 谢诗宏
 * @date 2021/3/9
 */
@Slf4j
@DubboService
public class OperationLogService extends ServiceImpl<OperationLogMapper, OperationLog> implements IOperationLogService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private OperationLogMapper operationLogMapper;

    @Override
    public Result<OperationLogDTO> query(long id) {
        return null;
    }

    @Override
    public Result<PageResponse<OperationLogDTO>> list(OperationLogReq operationLogReq) {
        IPage<OperationLog> iPage = new Page<>(operationLogReq.getPageNo(), operationLogReq.getPageSize());
       QueryWrapper<OperationLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(operationLogReq.getRecordId() != 0, OperationLog::getRecordId, operationLogReq.getRecordId())
                .eq(StringUtils.isNotBlank(operationLogReq.getType()), OperationLog::getType, operationLogReq.getType())
                .between(null != operationLogReq.getStartTime(), OperationLog::getGmtCreate, operationLogReq.getStartTime(), operationLogReq.getEndTime());
        IPage<OperationLog> logIPage = operationLogMapper.selectPage(iPage, queryWrapper);
        List<OperationLogDTO> operationLogDTOS = new ArrayList<>();
        logIPage.getRecords().forEach(log -> {
            OperationLogDTO logDTO = EntityUtil.copy(log, OperationLogDTO.class);
            logDTO.setUserName(redisTemplate.opsForValue().get(RedisConstants.ACCOUNT_QUERY_KEY + logDTO.getUserId()));
            operationLogDTOS.add(logDTO);
        });
        PageResponse<OperationLogDTO> result = new PageResponse<>(
                operationLogDTOS,
                logIPage.getCurrent() == 1,
                logIPage.getTotal() <= (logIPage.getCurrent() * logIPage.getSize()),
                logIPage.getTotal(),
                logIPage.getCurrent(),
                logIPage.getSize());
        return Result.success(result);
    }
}
